﻿// click vào Region name
function onOrgClick(link) {
    var regId = $(link).next().val();
    var parentNode = $(link).parent().parent();
    var status = parentNode.find("td").eq(4).text().toString().trim();
    
    //window.alert(status);
    if (status == "False") {
        if (window.confirm("Do you want to make this organization to active?")) {
            //ActivateRegion(link);
            window.location = "EditOrganization.aspx?orgId=" + regId;
        }
    }
    else {
        window.location = "EditOrganization.aspx?orgId=" + regId;
    }
}
// click vào Region name
function onDirClick(link) {
    var regId = $(link).next().val();
    var parentNode = $(link).parent().parent();
    var status = parentNode.find("td").eq(4).text().toString().trim();
    
    //window.alert(status);
    if (status == "False") {
        if (window.confirm("Do you want to make this Directorate to active?")) {
            //ActivateRegion(link);
            window.location = "UpdateDirectorate.aspx?dirId=" + regId;
        }
    }
    else {
        window.location = "UpdateDirectorate.aspx?dirId=" + regId;
    }
}
// click vào Region name
function onDepartClick(link) {
    var regId = $(link).next().val();
    var parentNode = $(link).parent().parent();
    var status = parentNode.find("td").eq(4).text().toString().trim();
    
    //window.alert(status);
    if (status == "False") {
        if (window.confirm("Do you want to make this Department to active?")) {
            //ActivateRegion(link);
            window.location = "UpdateDepartment.aspx?departId=" + regId;
        }
    }
    else {
        window.location = "UpdateDepartment.aspx?departId=" + regId;
    }
}
// click vào Region name
function onTeamClick(link) {
    var regId = $(link).next().val();
    var parentNode = $(link).parent().parent();
    var status = parentNode.find("td").eq(4).text().toString().trim();
    
    //window.alert(status);
    if (status == "False") {
        if (window.confirm("Do you want to make this Team to active?")) {
            //ActivateRegion(link);
            window.location = "UpdateTeam.aspx?teamId=" + regId;
        }
    }
    else {
        window.location = "UpdateTeam.aspx?teamId=" + regId;
    }
}
// click vào Region name
function onSMClick(link) {
    var regId = $(link).next().val();
    var parentNode = $(link).parent().parent();
    var status = parentNode.find("td").eq(5).text().toString().trim();
    var orgId = gup('orgId');
    var smCurrentPage = gup('smCurrentPage');
    var smInactive = gup('smInactive');
    
    //window.alert(status);
    if (status == "False") {
        if (window.confirm("Do you want to make this supporting material to active?")) {
            //ActivateRegion(link);
            window.location = "AddSupportingMaterial.aspx?SupportingMaterialID=" + regId + "&orgId=" + orgId + "&smCurrentPage=" + smCurrentPage + "&ActiveTabIdx=4" + "&smInactive=" + smInactive;
        }
    }
    else {
        window.location = "AddSupportingMaterial.aspx?SupportingMaterialID=" + regId + "&orgId=" + orgId + "&smCurrentPage=" + smCurrentPage + "&ActiveTabIdx=4" + "&smInactive=" + smInactive;
    }
}

// Active một Region
function ActivateRegion(link) {
    var regId = $(link).next().val();
    callServer("ShowOrganizationList.aspx",
                        "ActiveRegion",
                        "{id:" + regId + "}",
                        activateFinish);
}

// this is called when activation finished
function activateFinish(msg) {
    if (JSON.parse(msg.d).Success == "true") {
        window.location = "ShowOrganizationList.aspx";
    }
}

// call server method
function callServer(page, method, param, callBack) {
    $.ajax({
        type: "POST",
        url: page + "/" + method,
        data: param,
        contentType: "application/json",
        dataType: "json",
        success: callBack
    });
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
